package com.wjpeng.wms.service.impl;

import com.wjpeng.wms.domain.Brand;
import com.wjpeng.wms.mapper.BrandMapper;
import com.wjpeng.wms.query.QueryObject;
import com.wjpeng.wms.service.IBrandService;
import com.wjpeng.wms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class BrandServiceImpl implements IBrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public void saveOrUpdate(Brand d) {
        if(d.getId() == null){
            brandMapper.insert(d);
        }else{
            brandMapper.updateByPrimaryKey(d);
        }
    }

    @Override
    public void delete(Long id) {
        if(id != null){
            brandMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public Brand get(Long id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Brand> list() {
        return brandMapper.selectAll();
    }

    @Override
    public PageResult result(QueryObject qo) {
        Integer count = brandMapper.queryCount(qo);
        if(count == 0){
            return new PageResult(Collections.EMPTY_LIST,0,1,0);
        }
        return new PageResult(brandMapper.queryLimit(qo),count,qo.getCurrentPage(),qo.getPageSize());
    }
}
